package sg.edu.nus.iss.phoenix.maintainusers.service;

import sg.edu.nus.iss.phoenix.authenticate.dao.RoleDao;
import sg.edu.nus.iss.phoenix.authenticate.dao.UserDao;
import sg.edu.nus.iss.phoenix.authenticate.entity.Role;
import sg.edu.nus.iss.phoenix.authenticate.entity.User;
import sg.edu.nus.iss.phoenix.core.dao.DAOFactoryImpl;
import sg.edu.nus.iss.phoenix.core.exceptions.NotFoundException;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by A0119952X.
 */
public class UserService {

    private static final Logger logger =
            Logger.getLogger(UserService.class.getName());

    DAOFactoryImpl factory = new DAOFactoryImpl();
    UserDao udao;
    RoleDao rdao;

    /**
     * constructor
     */
    public UserService() {
        factory = new DAOFactoryImpl();
        udao = factory.getUserDAO();
        rdao = factory.getRoleDAO();
    }

    /**
     * constructor
     *
     * @param udao
     * @param rdao
     */
    public UserService(UserDao udao, RoleDao rdao) {
        this.udao = udao;
        this.rdao = rdao;
    }

    /**
     * change password
     *
     * @param user
     * @return true or false
     */
    public boolean changePassword(User user) {
        try {
            udao.save(user);
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, "user save", ex);
            return false;
        } catch (NotFoundException e) {
            return false;
        }

        return true;
    }

    /**
     * find user by ID
     *
     * @param userId
     * @return user
     */
    public User getUserByLoginId(String userId) {
        User currentLoginUser = null;
        try {
            currentLoginUser = udao.searchMatching(userId);
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, "user searchMatching", ex);
            return null;
        }
        return currentLoginUser;
    }

    /**
     * get all users
     *
     * @return all users
     */
    public ArrayList<User> getAllUsers() {
        ArrayList<User> allUsers = new ArrayList<User>();
        try {
            allUsers = (ArrayList<User>) udao.loadAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return allUsers;
    }

    /**
     * add user
     *
     * @param user
     * @return true or false
     */
    public boolean addUser(User user) {
        try {
            udao.create(user);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }

    /**
     * update user
     *
     * @param user
     * @return true or false
     */
    public boolean updateUser(User user) {
        try {
            udao.save(user);
        } catch (NotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * get all roles
     *
     * @return all roles
     */
    public ArrayList<Role> getAllRoles() {
        ArrayList<Role> allRoles = new ArrayList<Role>();
        try {
            allRoles = (ArrayList<Role>) rdao.loadAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return allRoles;
    }

    /**
     * delte user
     *
     * @param user
     * @return true or false
     */
    public boolean deleteUser(User user) {
        try {
            udao.delete(user);
        } catch (NotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * get all presenters
     *
     * @return all users that have role = 'presenter'
     */
    public List<User> loadPresenter() {
        try {
            return udao.loadPresenter();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * get all producers
     *
     * @return all users that have role = 'producer'
     */
    public List<User> loadProducer() {
        try {
            return udao.loadProducer();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }
}
